const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
var app = getApp();
Page({
  data: {
    keyword: '',
    schoolId: 0,
    searchStatus: true,
    currentSortType: 'default',
    currentSortOrder: 'desc',
    filterCategory: [],
    defaultKeyword: '请输入教练姓名',
    schoolFilter: false,
    starRingFilter: false,
    regionId: 0,
    regions: [],
    coachs: [],
    page: 1,
    limit: 30,
    totalPages: 1,
    endFlag: false,
    loadingFlag: false,
    categorieFilter: [
      { id: '5', name: "五星", checked: false },
      { id: '4', name: "四星", checked: false },
      { id: '3', name: "三星", checked: false },
      { id: '2', name: "二星", checked: false },
      { id: '1', name: "一星", checked: false },
      { id: '', name: "全部", checked: true }
    ],
    activeCategoryId: 0,
    filterSchoolList: [],
    navbar_bgcolor: wx.getStorageSync('navbar_bgcolor'),
  },


  getCoachs() {
    let that = this;
    if(that.data.page == 1){
      that.setData({
        coachs: []
      });
    }
    
    wx.showLoading({
      title: '加载中...',
    });
    util.request(api.CoachlList, {
      keyword: that.data.keyword,
      schoolId: that.data.schoolId,
      //取消区域id的限制
      // regionId: that.data.regionId,
      starRating: that.data.activeCategoryId,
      page: that.data.page,
      limit: that.data.limit
    }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          coachs: that.data.coachs.concat(res.data.list),
          totalPages: res.data.pages
        });
      }

      wx.hideLoading();
    });
  },


  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getCoachs();
    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
  },
  getRegions: function () {
    var that = this;
    util.request(api.RegionList).then(function (res) {
      console.log(res)
      if (res.errno === 0) {
        that.setData({
          regions: res.data.list
        });
      }

    });
  },
  onLoad: function () {
    var that = this;
    that.getSchools();
    that.getRegions();
  },
  onShow: function () {
    var that = this;
    this.setData({
      page: 1,
      coachs: []
    });
    that.getCoachs();
  },

  tabClick: function (e) {
    this.setData({
      activeCategoryId: e.currentTarget.id
    });
    this.getCoachs();
  },


  //事件处理函数
  bindViewTap: function (e) {
    var param = e.currentTarget.dataset;
    var id = param["id"];
    wx.navigateTo({
      url: '../detail/detail?id=' + id
    })
  },
  inputChange: function (e) {
    this.setData({
      keyword: e.detail.value,
    });
  },
  inputFocus: function () {
    this.setData({
      coachs: []
    });
  },
  goSearch() {
    this.setData({
      page: 1,
      coachs: []
    });
    this.getCoachs();
  },
  getSchools: function () {
    var that = this;
    util.request(api.zjlSchoolList, {
      regionId: that.data.regionId,
      starRating: 0,
      type: 3,
      page: 1,
      limit: 30
    }).then(function (res) {
      //console.log("RES::::::::"+JSON.stringify(res));
      if (res.errno === 0) {
        //console.log("result:::::"+JSON.stringify(res));
        //var list = res.data.list;
        var list = res.data;
        for (var item in list) {
          list[item]['checked'] = false;
        }
        //console.log("vvv::::"+JSON.stringify(list));
        //list = list+",{ id: '', name: '全部', checked: true }";
        that.setData({
          filterSchoolList: list
        });
      }
    });
  },
  openSortFilter: function (event) {
    let currentId = event.currentTarget.id;
    switch (currentId) {
      case 'schoolFilter':
        this.setData({
          schoolFilter: !this.data.schoolFilter,
          starRingFilter:false,
          currentSortType: 'schoolFilter',
          currentSort: 'add_time',
          currentSortOrder: 'desc'
        });
        break;
      case 'starRingFilter':
        this.setData({
          starRingFilter: !this.data.starRingFilter,
          schoolFilter: false,
          currentSortType: 'starRingFilter',
          currentSort: 'add_time',
          currentSortOrder: 'desc'
        });
        break;
      default:
    }
  },
  changeRegion: function (e) {
    this.setData({
      regionId: e.currentTarget.id
    });
    this.getSchools();
  },
  selectCategory: function (event) {
    let currentIndex = event.target.dataset.categoryIndex;
    console.log(currentIndex);
    let filterSchool = this.data.filterSchoolList;
    let currentSchool = null;
    for (let key in filterSchool) {
      if (key == currentIndex) {
        if (filterSchool[key].checked == true) {
          filterSchool[key].checked = false;
        } else {
          filterSchool[key].checked = true;
          currentSchool = filterSchool[key];
        }
      } else {
        filterSchool[key].checked = false;
      }
    }
    this.setData({
      filterSchoolList: filterSchool,
      schoolFilter: false,
      schoolId: currentSchool ? currentSchool.id : 0,
      page: 1,
      coachs: []
    });
    this.getCoachs();
  },
  selectStarRating: function (event) {
    let currentIndex = event.target.dataset.categoryIndex;
    let filterStarRating = this.data.categorieFilter;
    let currentStarRating = null;
    for (let key in filterStarRating) {
      if (key == currentIndex) {
        if (filterStarRating[key].checked == true) {
          filterStarRating[key].checked = false;
        } else {
          filterStarRating[key].checked = true;
          currentStarRating = filterStarRating[key];
        }
      } else {
        filterStarRating[key].checked = false;
      }
    }
    this.setData({
      categorieFilter: filterStarRating,
      starRingFilter: false,
      activeCategoryId: currentStarRating ? currentStarRating.id : 0,
      page: 1,
      coachs: []
    });
    this.getCoachs();
  }

})